<template>
  <div :class="{ 'slider-com': true, expand: menuStore.expandFold }">
    <div class="logo-box">
      <div class="logo-content">
        <img class="logo" src="/images/chatgpt-logo-3.png" alt="" />
        <div class="text">chatgpt中文网</div>
      </div>
    </div>
    <el-menu
      :default-active="indexAct"
      class="el-menu-slider"
      :collapse="menuStore.expandFold"
      background-color="#202123"
      @open="handleOpen"
      @close="handleClose"
    >
      <el-menu-item :index="item.index" v-for="(item, index) in menus" :key="index" @click="actMenuItem(item)"
        ><el-icon>
          <component :is="item.icon" />
        </el-icon>
        <span>{{ item.name }}</span>
      </el-menu-item>
    </el-menu>
  </div>
</template>
<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { useMenuStore } from '@/pinia/menu'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/pinia/user'
import { ElMessageBox } from 'element-plus'

const menuStore = useMenuStore()
const userStore = useUserStore()
const router = useRouter()
const route = useRoute()

let indexAct = computed(() => {
  let path = route.path
  return path
})

//点击导航
const actMenuItem = (item) => {
  if (item.index == '/home/api' || item.index == '/home/yongjin') {
    if (!userStore.userInfo) {
      ElMessageBox.confirm('此功能需要登录，去登录?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          router.push({ path: item.index })
        })
        .catch(() => {
          router.push({ path: '/home/index' })
        })
    } else {
      router.push({ path: item.index })
    }
  } else {
    router.push({ path: item.index })
  }

  // menuStore.changeTabs({ index: item.index, name: item.name })
}

const menus = ref([])
const autherMenu = () => {
  menus.value = menuStore.menus

  const userInfo = userStore.userInfo
  if (!userInfo || userInfo.role.code != 'gm') {
    menus.value = menus.value.filter((v) => v.name != '用户列表' && v.name != '上传Ai图片')
  }
}

// watch(
//   () => userStore.userInfo,
//   (newV) => {
//     if (newV) {
//       autherMenu()
//     }
//   },
//   { immediate: true, deep: true }
// )

onMounted(() => {
  autherMenu()
})
</script>
<style lang="scss" scoped>
.slider-com {
  position: fixed;
  left: 0;
  top: 0px;
  width: 200px;
  height: 100vh;
  background-color: #21212b;
  color: #fff;
  overflow-y: auto;
  transition: width 0.5s;
  padding-top: 0px;
  background: #202123;

  .logo-box {
    height: 50px;
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    margin-bottom: 13px;
    .logo-content {
      // border: 1px solid #fff;
      border-radius: 6px;
      @include flex();
      width: 100%;
      height: 50px;
      box-sizing: border-box;
      padding: 0 15px;
    }
    .logo {
      width: 30px;
      height: 30px;
      margin-right: 10px;
      border-radius: 300px;
    }

    .text {
      font-size: 14px;
    }
  }
  .el-menu-slider {
    padding: 10px;
    box-sizing: border-box;
  }

  :deep(.el-menu.el-menu-slider) {
    --el-menu-bg-color: #3c4145;
    --el-menu-hover-bg-color: #3c4145;
    --el-menu-active-color: #fff;
    --el-menu-text-color: #fff;
    --el-menu-hover-text-color: #fff;
    border-right: 0;

    .el-menu.el-menu--inline {
      --el-menu-bg-color: #17181c;
      --el-menu-hover-bg-color: rgba(255, 255, 255, 0.18);
      --el-menu-active-color: #fff;
      --el-menu-text-color: #fff;
      --el-menu-hover-text-color: #fff;
    }

    .el-menu-item {
      border-radius: 6px;
      height: 40px;
      margin-bottom: 15px;
    }

    .el-menu-item.is-active {
      background-color: rgba(255, 255, 255, 0.18) !important;
    }

    .el-menu-item:hover {
      background-color: rgba(255, 255, 255, 0.18);
    }

    .el-menu-item-group__title {
      padding: 0;
    }
  }
}

.slider-com.expand {
  width: 63px;
}
</style>
